package ilvonen.ihstatistics.repository;

import ilvonen.ihstatistics.models.Team;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

public class DBTeams {
	//define table name
		private static final String TABLE_NAME = "teams";
	//define tables column names
		private static final String KEY_ID = "_id";
		private static final String KEY_NAME = "name";
		private static final String KEY_ACTIVE = "active";
		private static final String KEY_USER_ID = "user_account_id";
	//define create statement for all your tables. Create new file (class) for each table.
		private static final String CREATE_STATEMENT = "create table if not exists " + TABLE_NAME + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME + " text not null, " + KEY_ACTIVE + " integer not null, " + KEY_USER_ID + " integer not null);";
	
	/*
	 * CLASS STATUS: READY?
	 */
	public DBTeams(){}
	/*
	 * 
	 */
	public String create(){
		return CREATE_STATEMENT;
	}
	/*
	 * 
	 */
	public long insert(String name, int active, int uaid, DBAdapter db){
		
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, name);
		initialValues.put(KEY_ACTIVE, active);
		initialValues.put(KEY_USER_ID, uaid);
		
		return db.insertRecord(TABLE_NAME, initialValues);
		
	}
	/*
	 * 
	 */
	public boolean delete(long rowId, DBAdapter db){
		return db.deleteRecord(TABLE_NAME, KEY_ID, rowId);
	}
	/*
	 * 
	 */
	public List<Team> getAllTeams(DBAdapter db){
		Cursor c;
		List<Team> teams = new ArrayList<Team>();
		c = db.getAllRecords(TABLE_NAME, new String[]{KEY_ID,KEY_NAME,KEY_ACTIVE,KEY_USER_ID});
		while(c.moveToNext()){
			Team t = new Team();
			t.set_id(c.getInt(0));
			t.set_name(c.getString(1));
			t.set_active(c.getInt(2));
			t.set_userAccountId(c.getInt(3));
			teams.add(t);
		}
		return teams;
	}
	/*
	 * 
	 */
	public List<Team> getAllTeamsByUserAccountId(DBAdapter db, long ROW_ID){
		Cursor c;
		List<Team> teams = new ArrayList<Team>();
		c = db.getAllRecordsById(TABLE_NAME, new String[]{KEY_ID,KEY_NAME,KEY_ACTIVE,KEY_USER_ID}, KEY_USER_ID, ROW_ID);
		while(c.moveToNext()){
			Team t = new Team();
			t.set_id(c.getInt(0));
			t.set_name(c.getString(1));
			t.set_active(c.getInt(2));
			t.set_userAccountId(c.getInt(3));
			teams.add(t);
		}
		return teams;
	}
	public Team getTeamById(DBAdapter db, long ROW_ID){
		Team t = null;
		Cursor c;
		c = db.getRecord(TABLE_NAME, new String[]{KEY_ID,KEY_NAME,KEY_ACTIVE,KEY_USER_ID}, KEY_ID, ROW_ID);
		if(c != null){
			t = new Team();
			c.moveToFirst();
			t.set_id(c.getInt(0));
			t.set_name(c.getString(1));
			t.set_active(c.getInt(2));
			t.set_userAccountId(c.getInt(3));
		}
		return t;
	}

}
